package cn.jx.asc.controller;

import cn.jx.asc.common.config.InjectionConfig;
import cn.jx.asc.common.utils.FileUtils;
import cn.jx.asc.pojo.entity.Mountain;
import cn.jx.asc.pojo.entity.Pipe;
import cn.jx.asc.service.CustomDataService;
import cn.jx.asc.service.MountainService;
import cn.jx.asc.service.PipeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 通用请求
 *
 * @author lhg
 */
@Api(value = "通用请求", tags = {"通用请求相关接口"})
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    PipeService pipeService;

    @Autowired
    MountainService mountainService;

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @ApiOperation(value = "通用下载", notes = "通用下载", httpMethod = "GET")
    @GetMapping("download")
    public void fileDownload(String fileName, HttpServletResponse response){
        try {
            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            String filePath = InjectionConfig.getDownloadPath() + fileName;
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
        } catch (IOException e) {
            log.error("下载文件失败", e);
        }

    }


}
